//bange_font.cpp
//Copyright (C) 2010-2011 Oscar (.teri) Triano

//This program is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with this program.  If not, see <http://www.gnu.org/licenses/>.

#include <bange_box.hpp>
#include <bange_font.hpp>

void bange::font::RegisterVM(lua_State *vm){
	luaL_Reg function[] = {
	{"LoadFont", bangefont_LoadFont},
	{NULL, NULL}};
	luaL_register(vm, NULL, function);
}

bange::font::~font(){
	cout << "bange::font destructor for: " << this << endl;
}

//bange.

static int bangefont_LoadFont(lua_State *vm){
	//filename
	if (!lua_isstring(vm, 1)){
		cout << "bange.LoadFont: Please, pass a string as first argument" << endl;
		lua_pushnil(vm);
		return 1;
	}
	bange::font *font = new bange::font;
	char *stringfont = NULL;
	PHYSFS_sint64 readed = bange::readbinaryfrompackage(lua_tostring(vm, 1), &stringfont);
	if (readed < 1){
		cout << "bange.LoadFont: Isn't possibble read font " << lua_tostring(vm, 1) << " from pacakge." << endl;}
	if (!font->LoadFromMemory(stringfont, static_cast<std::size_t>(readed))){
		cout << "bange.LoadFont: Error loading font" << endl;
		lua_pushnil(vm);
		delete font;
		return 1;
	}
	bange::BuildProxy(LUA_REFNIL, dynamic_cast<bange::base *>(font), vm);
	return 1;
}
